package com.zed.game.answer.fetures;

import com.mybatisflex.annotation.Id;
import com.mybatisflex.annotation.Table;
import com.zed.api.common.Serializer;
import com.zed.api.games.answer.show.AnswerShowWorld;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.Serial;
import java.io.Serializable;
import java.util.Objects;

/**
 * 答题序列化器
 *
 * @author Zed
 */
@Table("answer_show_world")
class AnswerShowWorldEntity implements Serializable {
    @Serial
    private static final long serialVersionUID = -6308792854921969492L;

    private static final Serializer SERIALIZER = new AnsweShowWorldSerializer();

    private static final Logger LOGGER = LoggerFactory.getLogger(AnswerShowWorldEntity.class);

    @Id
    private String id;

    private byte[] values;

    /**
     * 用于序列化
     */
    protected AnswerShowWorldEntity() {

    }

    public AnswerShowWorldEntity(AnswerShowWorld answerShowWorld) {
        Objects.requireNonNull(answerShowWorld, "\"answerShowWorld\" cannot be null.");
        this.id = answerShowWorld.getId();
        try {
            this.values = SERIALIZER.serial(answerShowWorld);
        } catch (IOException e) {
            LOGGER.info("序列化失败 -> {}", e.getMessage());
        }
    }

    /**
     * 获取答题
     *
     * @return {@link AnswerShowWorld}
     */
    public AnswerShowWorld getAnswerShowWorld() {
        try {
            return SERIALIZER.deSerial(this.getValues(), AnswerShowWorld.class);
        } catch (Exception e) {
            LOGGER.info("序列化失败 -> {}", e.getMessage());
            return null;
        }
    }

    /**
     * 获取答题
     *
     * @return {@link C}
     */
    public <C extends AnswerShowWorld> C getAnswerShowWorld(Class<C> clazz) {
        try {
            return SERIALIZER.deSerial(this.getValues(), clazz);
        } catch (Exception e) {
            LOGGER.info("序列化失败 -> {}", e.getMessage());
            return null;
        }
    }

    /**
     * 获取字节数组
     *
     * @return {@link byte[]}
     */
    public byte[] getValues() {
        return this.values;
    }

    /**
     * 获取主键
     *
     * @return {@link String}
     */
    public String getId() {
        return this.id;
    }
}
